<?php
/**
 * 应用模型层
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK_Model
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: GoodsModel.class.php 7615 2011-06-03 08:11:46Z yemusheng $
 * 
 */
class GoodsModel extends CommonModel {
	
	//首页顶部推荐应用 
	public function getIndexTopRcommend(){
		$map = $this->makeWhere(0, array('hot','new', 'best'));
		$row = $this->table($this->tablePrefix.'goods AS g')->field('g.goods_id as id, g.goods_name as title, g.short_title, g.goods_icon as icon')
				->where($map)
				->limit(25)
				->order('g.sort_order DESC, g.add_time DESC')
				->findAll();
		foreach ($row as $key=>$app){
			$row[$key]['url'] = U('thinkapk://Android/index', array('id'=>$app['id']));
			$row[$key]['short_title'] = !empty($app['short_title'])?$app['short_title']:$app['title'];
			$row[$key]['icon'] = $this->getIconPath($app['icon']);
			unset($row[$key]['goods_name'], $app);
		}
		unset($map);
		return $row;
	}
	
	//生成条件
	public function makeWhere($cat_id=0, $type=''){
		$map = array(
			'g.is_delete'=>0,
			'g.is_on_sale'=>1
		);
		$_type = array('hot', 'new', 'best');
		if(is_array($type)){
			foreach ($type as $tp){
				if(in_array($tp, $_type)) $map['g.is_'.$tp]=1;
			}
		}else{
			if(in_array($type, $_type)) $map['g.is_'.$type]=1;
		}
		if(is_array($cat_id)){
			$map['g.cat_id'] = array('in', $cat_id);
		}else{
			if($cat_id) $map['g.cat_id'] = $cat_id;
		}
		unset($_type, $type, $cat_id);
		return $map;
	}
	
	//首页新应用
	public function newApp($cat_id=0, $type='', $limit=8){
		if($cat_id) $cat_id = D('Category')->getChiledrenId($cat_id);
		$map = $this->makeWhere($cat_id, $type);
		$row = $this->table($this->tablePrefix.'goods AS g')->field('g.goods_id as id, g.goods_name as title, g.add_time, g.short_title, g.recommend, g.goods_icon as icon, g.short_desc as gdesc')
			->where($map)
			->limit($limit)
			->order('g.add_time DESC, g.goods_id DESC')
			->findAll();
		if($row){
			foreach($row as $key=>$app){
				$row[$key]['url'] = U('thinkapk://Android/index', array('id'=>$app['id']));
				$row[$key]['short_title'] = !empty($app['short_title'])?$app['short_title']:$app['title'];
				$row[$key]['icon'] = $this->getIconPath($app['icon']);
				$row[$key]['time'] = $this->formatTime($app['add_time']);
				//$row[$key]['gdesc'] = strip_tags($app['gdesc']); //str_replace(array(' ', "\t"), array('', ''), $app['gdesc']);
				unset($row[$key]['goods_name'], $row[$key]['add_time'], $app);
			}
		}
		unset($map);
		return $row;
	}
	
	//推荐应用
	public function recommendApp($cat_id=0, $limit=9, $re=array('best'), $order=''){
		if(in_array($cat_id, array(C('APP_ID'), C('GAME_ID')))) $cat_id = D('Category')->getChiledrenId($cat_id);
		$map = $this->makeWhere($cat_id, $re);
		if(empty($order)) $order = 'g.sort_order DESC, g.add_time DESC, g.goods_id DESC';
		$row = $this->table($this->tablePrefix.'goods AS g')->field('g.goods_id as id, gc.down_total, g.goods_name as title, g.short_title, g.add_time,  g.goods_icon as icon, g.recommend, g.short_desc as gdesc')
			->where($map)
			->join($this->tablePrefix.'goods_count as gc ON gc.goods_id=g.goods_id')
			->group('g.goods_id')
			->limit($limit)
			->order($order)
			->findAll();
		if($row){
			foreach($row as $key=>$app){
				$row[$key]['url'] = U('thinkapk://Android/index', array('id'=>$app['id']));
				$row[$key]['short_title'] = !empty($app['short_title'])?$app['short_title']:$app['title'];
				$row[$key]['icon'] = $this->getIconPath($app['icon']);
				$row[$key]['time'] =  $this->formatTime($app['add_time']);
				unset($row[$key]['goods_name'], $app);
			}
		}
		unset($map);
		return $row;
	}
	
	//推荐应用 WAP页面
	public function recommendAppForWap($cat_id=0, $limit=10, $re=array('best'), $order=''){
		if(in_array($cat_id, array(C('APP_ID'), C('GAME_ID')))) $cat_id = D('Category')->getChiledrenId($cat_id);
		$map = $this->makeWhere($cat_id, $re);
		if(empty($order)) $order = 'g.sort_order DESC, g.add_time DESC, g.goods_id DESC';
		$row = $this->table($this->tablePrefix.'goods AS g')->field('g.goods_id as id, gc.down_total, gca.cat_name, ga.size, ga.app_url, g.goods_name as title, g.short_title, g.add_time,  g.goods_icon as icon, g.recommend, g.short_desc as gdesc')
			->where($map)
			->join($this->tablePrefix.'goods_count as gc ON gc.goods_id=g.goods_id')
			->join($this->tablePrefix.'app as ga ON ga.goods_id=g.goods_id')
			->join($this->tablePrefix.'category as gca ON gca.cat_id=g.cat_id')
			->group('g.goods_id')
			->limit($limit)
			->order($order)
			->findAll();
		if($row){
			foreach($row as $key=>$app){
				$row[$key]['url'] = U('thinkapk://Android/index', array('id'=>$app['id']));
				$row[$key]['cat_url'] = U('thinkapk://Class/index', array('id'=>$app['cat_id']));
				$row[$key]['short_title'] = !empty($app['short_title'])?$app['short_title']:$app['title'];
				$row[$key]['icon'] = $this->getIconPath($app['icon']);
				$row[$key]['time'] =  $this->formatTime($app['add_time']);
				unset($row[$key]['goods_name'], $app);
			}
		}
		unset($map);
		return $row;
	}
	
	//大家正在下
	public function downloading($limit=1){
		$map = $this->makeWhere();
		$row = $this->table($this->tablePrefix.'goods AS g')->field('g.goods_id as id, gc.down_total, g.goods_name as title, g.last_down, g.short_title,  g.goods_icon as icon')
			->where($map)
			->join($this->tablePrefix.'goods_count as gc ON gc.goods_id=g.goods_id')
			->group('g.goods_id')
			->limit($limit)
			->order('g.last_down DESC, g.goods_id DESC')
			->findAll();
		if($row){
			foreach($row as $key=>$app){
				$row[$key]['url'] = U('thinkapk://Android/index', array('id'=>$app['id']));
				$row[$key]['short_title'] = !empty($app['short_title'])?$app['short_title']:$app['title'];
				$row[$key]['icon'] = $this->getIconPath($app['icon']);
				$row[$key]['time'] = $this->formatTime($app['last_down']);
				unset($row[$key]['goods_name'], $row[$key]['last_down'], $app);
			}
		}
		unset($map);
		return $row;
	}
}
?>